<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/aui2/aui.css" />
    <style>
        body{
            
        }
        .choices{
            height:180px;
            margin:0.75rem;
            display:-webkit-flex;
            -webkit-flex-direction: column;
            display: -webkit-box;
            -webkit-box-orient:vertical;
            margin-top:2rem;
        }
        .choice-row{
            display:-webkit-flex;
            -webkit-flex:1;
            -webkit-flex-direction: row;
            display: -webkit-box;
            -webkit-box-flex:1;
        }
        .choice-item{
            display:-webkit-flex;
            -webkit-flex:1;
            display: -webkit-box;
            -webkit-box-flex:1;
            border:2px solid #009688;
            margin:0.2rem;
            -webkit-justify-content:center;
            -webkit-align-items:center;
            -webkit-box-align:center;
            -webkit-box-pack:center;
            font-size:24px;
            font-weight: bold;
            border-radius: 8px;
            color:#009688;
            width:100%;
        }
        .bold{
           font-weight: bold;
           font-family: "黑体";
        }
        .active{
           color:#fff;
           background-color: #009688;
        }
        .aui-content-padded{
           margin: 1.5rem;
        }
    </style>
</head>
<body>
    <div class="choices">
        <div class="choice-row"> 
            <div tapmode class="choice-item active">6元</div>
            <div tapmode class="choice-item">68元</div>
            <div tapmode class="choice-item">208元</div>
        </div>
        <div class="choice-row">
            <div tapmode class="choice-item">388元</div>
            <div tapmode class="choice-item">698元</div>
            <div tapmode class="choice-item">998元</div>
        </div>
    </div>
    <div class="aui-content-padded">
       <p><div class="aui-btn aui-btn-success aui-btn-block bold" tapmode onclick="recharge();">立即充值</div></p>
    </div>
</body>
<script data-main="../../script/main" src="../../script/require.js"></script>
<script type="text/javascript">
    var token;
    apiready = function(){
        require(["zepto","common"],function(){
            $(".choice-item").on('click',function(){
                $('.active').removeClass('active');
                $(this).addClass('active');
            });
            $zb.prefs.getPrefs('token',function(ret){
                token = ret.value;
            })
        })
    };
    
    function recharge(){
       
        api.actionSheet({
            title: '请选择支付方式',
            cancelTitle: '取消',
            buttons: ['微信', '支付宝']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if(index==2){
                ajaxforRecharge(2);
            }
            if(index==1){
                ajaxforRecharge(1);
            }   
        });
    }
    
    function ajaxforRecharge(index){
        var text = $('.active').html();
        var beans = text.substring(0,text.length-1)*1;
        var s_data = {
            values : {
                token : token,
                pay_type : index,
                beans : beans
            }
        }
        $zb.requestForData(s_data,'order','recharge',function(ret){
            if(ret.status=="OK"){
                if(index==2){
                    alipay(ret);
                }else{
                    wxpay(ret);
                }
            }else{
                api.toast({
                    msg:ret.msg
                });
            }
        })
    }
    
    function alipay(ret){
        var aliPayPlus = api.require('aliPayPlus');
        var pay_return = ret.data.pay_return;
        var trade_number = ret.data.trade_number;
        aliPayPlus.payOrder({
            orderInfo: pay_return
        }, function(ret, err) {
            var code = ret.code;
            if(code==9000){
                var cs_data={
                    values:{
                        token:token,
                        trade_number :trade_number
                    }
                }
                $zb.requestForData(cs_data,'order','getPayStatus',function(ret1){
                    if(ret1.status=="OK"){
                        api.execScript({
                            name:'rs_detail_win',
                            frameName:'rs_pay',
                            script: 'refreshBean();'
                        });
                    }else{
                        api.toast({
                            msg:ret1.status
                        });
                    }   
                })
            }else if(code==8000||code==6004){
                api.toast({
                    msg:'支付结果未知，请稍后查询'
                });
            }else if(code==4000){
                api.toast({
                    msg:'订单支付失败'
                });
            }else if(code==5000){
                api.toast({
                    msg:'重复请求'
                });
            }
        });
    }
    
    function wxpay(ret){
        var wxPay = api.require('wxPay');
        var orderId = ret.data.pay_return.prepayid;
        var mchId = ret.data.pay_return.partnerid;
        var nonceStr = ret.data.pay_return.noncestr;
        var timeStamp = ret.data.pay_return.timestamp;
        var sign = ret.data.pay_return.sign;
        var trade_number = ret.data.trade_number;
        
        wxPay.payOrder({
            orderId: orderId,
            mchId: mchId,
            nonceStr: nonceStr,
            timeStamp: timeStamp,
            sign: sign
        }, function(ret, err) {
            if (ret.status) {
                var cs_data={
                    values:{
                        token:token,
                        trade_number :trade_number
                    }
                }
                $zb.requestForData(cs_data,'order','getPayStatus',function(ret1){
                    if(ret1.status=="OK"){
                        api.execScript({
                            name:'rs_detail_win',
                            frameName:'rs_pay',
                            script: 'refreshBean();'
                        });
                    }else{
                        api.toast({
                            msg:ret1.status
                        });
                    }   
                })
            } else {
                if(err.code==-1||err.code==1){
                    api.toast({
                        msg:'充值失败'
                    });
                }
            }
        });
    }
</script>
</html>